package LeetCode.month02.day32;

import java.util.Arrays;

public class No2009 {
    public static void main(String[] args) {
        System.out.println(minOperations(new int[]{8,5,9,9,8,4}));
    }
    public static int minOperations(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        int m = 1;
        for (int i = 1; i < n; i++) {
            if(nums[i-1]!=nums[i]){
                nums[m++]=nums[i];
            }
        }
        int left = 0;
        int res = 0;
        for (int right = 0; right < m; right++) {
            while(nums[left]<nums[right]-n+1){
                left++;
            }
            res = Math.max(res,right-left+1);
        }
        return n-res;
    }
}
